package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsHotWord;
import com.yuke.cloud.service.cmc.service.CmsHotWordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zys on 2019/01/18.
 */
@Api(value = "WEB - CmsHotWordController", tags = "处理CmsHotWord相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/hot_word")
public class CmsHotWordController extends BaseController {
    @Resource
    private CmsHotWordService cmsHotWordService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsHotWord", value = "该数据记录的POJO") @RequestBody CmsHotWord cmsHotWord) {
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        cmsHotWord.setUpdateUser(loginAuthDto.getUserId());
        cmsHotWordService.save(cmsHotWord);
        return WrapMapper.ok(cmsHotWord.getHotWordId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsHotWordService.deleteById(id);
        return WrapMapper.ok();
    }


    @PostMapping("/batchSave")
    @ApiOperation(httpMethod = "POST", value = "批量修改")
    public Wrapper batchSave(@RequestBody List<CmsHotWord> cmsHotWords) {
        for (CmsHotWord cmsHotWord : cmsHotWords) {
            LoginAuthDto loginAuthDto = getLoginAuthDto();
            cmsHotWord.setUpdateUser(loginAuthDto.getUserId());
            cmsHotWordService.update(cmsHotWord);
        }
        return WrapMapper.ok();
    }

    //批量删除
    @PostMapping("/batchDelete")
    @ApiOperation(httpMethod = "POST", value = "批量删除")
    public Wrapper batchDelete(@RequestBody List<Long> cmsHotWords) {
        for (Long cmsHotWord : cmsHotWords) {
            cmsHotWordService.deleteById(cmsHotWord);
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsHotWord", value = "需要修改的内容") @RequestBody CmsHotWord cmsHotWord) {
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        cmsHotWord.setUpdateUser(loginAuthDto.getUserId());
        cmsHotWordService.update(cmsHotWord);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsHotWord> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsHotWord cmsHotWord = cmsHotWordService.findById(id);
        return WrapMapper.ok(cmsHotWord);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsHotWord>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                              @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        Example example = new Example(CmsHotWord.class);
        example.setOrderByClause("search_type asc,sort asc");
        List<CmsHotWord> list = cmsHotWordService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //唯一性检查
    @GetMapping("/ifUnique")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper ifUnique(@RequestParam("searchWord") String searchWord,
                            @RequestParam(value = "hotWordId", required = false) Long hotWordId
    ) {
        Example example = new Example(CmsHotWord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("searchWord", searchWord);
        if (hotWordId != null && !hotWordId.toString().equals("")) {
            criteria.andNotEqualTo("hotWordId", hotWordId);
        }
        List<CmsHotWord> list = cmsHotWordService.selectByExample(example);
        if (list.size() == 0) {
            return WrapMapper.ok(true);
        } else {
            return WrapMapper.ok(false);
        }
    }


}
